<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Models\Company;
use App\Models\CompanyAuthInfo;
use App\Models\CompanyAuth;
use Illuminate\Support\Facades\Response;

class CompanyAuthInfoController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        if(!$this->user->can('viewCompanyAuth')) abort(405);
        // 搜索处理
        $post = $request->all();
        $info = new Company;
        if (!empty($post)) {
            $infos = $info->where('name', 'LIKE', '%'.$post['company_name'].'%')
                          ->where('auth_id', $post['auth_id'])
                          ->where(function($query) use ($post) {
                                $query->where('auth_at', '>=', $post['auth_at_start'])
                                      ->where('auth_at', '<=', $post['auth_at_end']);
                            })
                            ->paginate(20);
        } else {
            $infos = $info->paginate(20);
        }

        $auths = CompanyAuth::all();
        return view('admin.enterprise.companyauthinfo.index')
                ->with('infos', $infos)
                ->with('auths',$auths);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('editCompanyAuth')) abort(405);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!$this->user->can('editCompanyAuth')) abort(405);
        
        $company_id = $request->get('company_id', 0);
        $status = $request->get('status', 0);
        $reson = $request->get('reson', '');
        $auth_info = CompanyAuthInfo::find($company_id);
        $auth_info->status = $status;
        $auth_info->reson = $reson;
        $auth_info->save();
        return redirect("admin/enterprise/company_auth");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        if(!$this->user->can('viewCompanyAuth')) abort(405);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewCompanyAuth')) abort(405);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 异步查看企业法人身份证扫描件
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function viewID(Request $request)
    {
        if(!$this->user->can('viewCompanyAuth')) 
            return $this->formatJson('errot','抱歉，您没有相关操作权限！');

        $company_id = $request->get('company_id', 0);
        $ID_url = CompanyAuthInfo::where('company_id', $company_id)->pluck('ID_url');
        return Response::json(array('status'=>'success','msg'=>'', 'data' => $ID_url ));
    }

    /**
     * 异步查看营业执照
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function viewPermits(Request $request)
    {
        if(!$this->user->can('viewCompanyAuth')) 
            return $this->formatJson('error','抱歉，您没有相关操作权限！');
        
        $company_id = $request->get('company_id', 0);
        $permits = CompanyAuthInfo::where('company_id', $company_id)->pluck('permits');
        return Response::json(array('status'=>'success','msg'=>'', 'data' => $permits ));
    }

    /**
     * 审核
     * @return [type] [description]
     */
    public function changeStatus(Request $request)
    {
        if(!$this->user->can('editCompanyAuth')) 
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        
        $company_id = $request->get('company_id', 0);
        $status = $request->get('status', 0);
        $auth_info = CompanyAuthInfo::find($company_id);
        $auth_info->status = $status;
        $auth_info->save();
        return Response::json(array('status'=>'success','msg'=>''));
    }

    /**
     * 获取审核不通过的理由
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getReson(Request $request)
    {
        if(!$this->user->can('editCompanyAuth'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        
        $company_id = $request->get('company_id', 0);
        $reson = CompanyAuthInfo::where('company_id', $company_id)->pluck('reson');
        return Response::json(array('status'=>'success','msg'=>'', 'data' => $reson ));
    }
}
